<?php
include_once 'weixinEncrypt/WXBizMsgCrypt.php';
defined('BASEPATH') OR exit('No direct script access allowed');

class Mytest extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
            $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.urldecode($_SERVER['QUERY_STRING']);
            $Url_data=  $this->Url_Get($url);
            //var_dump("Url_data = ".$Url_data);
            file_put_contents('./logwechat.txt',"1".$_SERVER['QUERY_STRING']."||".$url." url = ".$url."\r\n",FILE_APPEND);
			
            $corpId = "wx48ac7010d65a20d7"; //企业号ID
            $token = "mTnfg94QSuwTicJ";
            $encodingAesKey = "iLSXVnjzbk24P2kMiH4aPSn0WX38b7uKN5CUfUWNYd8";
            $sVerifyMsgSig = $_GET["msg_signature"];
            $sVerifyTimeStamp = $_GET["timestamp"];
            $sVerifyNonce = $_GET["nonce"];
            if(isset($Url_data["echostr"])){
                $sVerifyEchoStr = $Url_data["echostr"];
            }  else {
                $sVerifyEchoStr = "";
            }
            $wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId);
            $errCode = $wxcpt->VerifyURL($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sVerifyEchoStr, $sEchoStr);
			var_dump($errCode); 
            if ($errCode == 0) {
                    file_put_contents('./logwechat.txt',"1".$_SERVER['QUERY_STRING']."||".$url."\r\n",FILE_APPEND);
                    print($sEchoStr);
            } else {
                    file_put_contents('./logwechat.txt',"2".$_SERVER['QUERY_STRING']."||".$url."\r\n",FILE_APPEND);
                    print($errCode);
            }
            $sMsg = "";
            if ($errCode == 0) {   
                $xml = new DOMDocument();  
                $xml->loadXML($sMsg);   
                $reqToUserName = $xml->getElementsByTagName('ToUserName')->item(0)->nodeValue;  
                $reqFromUserName = $xml->getElementsByTagName('FromUserName')->item(0)->nodeValue;  
                $reqCreateTime = $xml->getElementsByTagName('CreateTime')->item(0)->nodeValue;  
                $reqMsgType = $xml->getElementsByTagName('MsgType')->item(0)->nodeValue;  
                $Event = $xml->getElementsByTagName('Event')->item(0)->nodeValue;  
                $EventKey = $xml->getElementsByTagName('EventKey')->item(0)->nodeValue;  
                $ScanCodeInfo = $xml->getElementsByTagName('ScanCodeInfo')->item(0)->nodeValue; 
                $ScanType = $xml->getElementsByTagName('ScanType')->item(0)->nodeValue;  
                $ScanResult = $xml->getElementsByTagName('ScanResult')->item(0)->nodeValue;  
                $AgentID = $xml->getElementsByTagName('AgentID')->item(0)->nodeValue; 
                $myContent = "我的促使数据";  
                $sRespData = "<xml><ToUserName><![CDATA[".$reqToUserName."]]></ToUserName>
                                <FromUserName><![CDATA[".$reqFromUserName."]]></FromUserName>
                                <CreateTime>".$reqCreateTime."</CreateTime>
                                <MsgType><![CDATA[".$reqMsgType."]]></MsgType>
                                <Event><![CDATA[".$Event."]]></Event>
                                <EventKey><![CDATA[".$EventKey."]]></EventKey>
                                <ScanCodeInfo><ScanType><![CDATA[".$ScanCodeInfo."]]></ScanType>
                                <ScanResult><![CDATA[".$ScanResult."]]></ScanResult>
                                </ScanCodeInfo>
                                <AgentID>".$AgentID."</AgentID>
                                </xml>";
                $sEncryptMsg = ""; //xml格式的密文  
                $errCode = $wxcpt->EncryptMsg($sRespData, $sReqTimeStamp, $sReqNonce, $sEncryptMsg);  
                if ($errCode == 0) {  
                //file_put_contents('smg_response.txt', $sEncryptMsg); //debug:查看smg  
                    print($sEncryptMsg);  
                } else {  
                    print($errCode . "\n\n");  
                } 
            }
	}
        function Url_Get($str){
            $data = array();
                $cs_str=explode('?',$str);
            $parameter = explode('&',end($cs_str));
            //var_dump("parameter = ".$parameter);
            foreach($parameter as $val){
                        if(count(explode("msg_signature",$val))>1){
                                $data["msg_signature"] = str_replace("msg_signature=","",$val);
                        }
                        if(count(explode("timestamp",$val))>1){
                                $data["timestamp"] = str_replace("timestamp=","",$val);
                        }
                        if(count(explode("nonce",$val))>1){
                                $data["nonce"] = str_replace("nonce=","",$val);
                        }
                        if(count(explode("echostr",$val))>1){
                                $data["echostr"] = str_replace("echostr=","",$val);
                        }      
            }
            return $data;
        }
}
